package test06.stream.demo02.t09;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author ZQX
 * @date 2025-04-08
 */
public class MainTest04 {
    public static void main(String[] args) {
        // 第一：定义数据源，并获取对应的流对象
        Student s1 = new Student("张三", "男", 18, 55);
        Student s2 = new Student("李四", "女", 28, 63);
        Student s3 = new Student("王五", "男", 38, 50);
        Student s4 = new Student("赵六", "女", 48, 99);
        List<Student> list = Arrays.asList(s1, s2, s3, s4);

        // 第二：操作 - 收集流中的数据
        // 1.根据性别分组
        Map<String, List<Student>> map = list.stream()
                .collect(Collectors.groupingBy(Student::getSex));

        // key:分组字段，value:分组结果
        map.forEach((key, value) -> {
            System.out.println(key + ":" + value.size());
        });

    }
}
